package com.cn.demo4.dao.impl;

import com.cn.demo4.dao.LayoutDao;
import com.cn.demo4.model.LayoutModel;
import com.cn.demo4.util.GetSqlSession;
import com.cn.demo4.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class LayoutDaoImpl implements LayoutDao {
    @Override
    public List<LayoutModel> findAll(LayoutModel layoutModel) {
//        String sql = "select * from layout";
//        StringBuilder sql = new StringBuilder();
//        sql.append("select * from layout where true ");
//        if (!"".equals(layoutModel.getTitle())) {
//            sql.append(" and title like '%").append(layoutModel.getTitle()).append("%'");
//        }
//        if (!"".equals(layoutModel.getContent())) {
//            sql.append(" and content like '%").append(layoutModel.getContent()).append("%'");
//        }
//        if (!"".equals(layoutModel.getStartTime()) && "".equals(layoutModel.getEndTime())) {
//            sql.append(" and update_time >= '").append(layoutModel.getStartTime()).append("' ");
//        }
//        if ("".equals(layoutModel.getStartTime()) && !"".equals(layoutModel.getEndTime())) {
//            sql.append(" and update_time <= '").append(layoutModel.getEndTime()).append("' ");
//        }
//        if (!"".equals(layoutModel.getStartTime()) && !"".equals(layoutModel.getEndTime())) {
//            sql.append(" and update_time between '").append(layoutModel.getStartTime()).append("' and '").append(layoutModel.getEndTime()).append("'");
//        }
//        if (layoutModel.getStatus() != -1) {
//            if (layoutModel.getStatus() == 1) {
//                sql.append(" and status=1");
//            } else {
//                sql.append(" and status is null and status=0");
//            }
//        }
//        sql.append(" order by id desc");
//        //limit ((curPage-1)*pageSize,pageSize )
//        sql.append(" limit ").append((layoutModel.getPageModel().getCurPage() - 1) * layoutModel.getPageModel().getPageSize()).append(",").append(layoutModel.getPageModel().getPageSize());
//        ResultSet resultSet = JDBC.select(sql.toString());
//        return getData(resultSet);
        return GetSqlSession.getSqlSession().selectList("com.cn.demo4.dao.LayoutDao.findAll",layoutModel);

    }

    @Override
    public int add(LayoutModel layoutModel) {
//        String sql = "insert into layout(title,img,content,status,second_title,second_content,third_title,create_time,update_time)" +
//                "values('" + layoutModel.getTitle() + "','" + layoutModel.getImg() + "'," +
//                "'" + layoutModel.getContent() + "'," + layoutModel.getStatus() + ",'" + layoutModel.getSecondTitle() + "'," +
//                "'" + layoutModel.getSecondContent() + "','" + layoutModel.getThirdTitle() + "'," +
//                "now(),now())";
//        return JDBC.upDate(sql);
        return  GetSqlSession.getSqlSession().insert("com.cn.demo4.dao.LayoutDao.add",layoutModel);
    }

    @Override
    public LayoutModel findById(Integer id) {
//        String sql = "select * from layout where id=" + id;
//        ResultSet resultSet = JDBC.select(sql);
//        List<LayoutModel> list = getData(resultSet);
//        return list.size() > 0 ? list.get(0) : null;
        List<LayoutModel> list = GetSqlSession.getSqlSession().selectList("com.cn.demo4.dao.LayoutDao.findById",id);
        return list.size() > 0 ? list.get(0) : null;

    }

    @Override
    public int update(LayoutModel layoutModel) {
//        String sql = "update layout set title='" + layoutModel.getTitle() + "',";
//        if (layoutModel.getImg() != null && !layoutModel.getImg().equals("undefined")) {
//            sql += "img='" + layoutModel.getImg() + "',";
//        }
//        sql += "content='" + layoutModel.getContent() + "',status=" + layoutModel.getStatus() + "," +
//                "second_title='" + layoutModel.getSecondTitle() + "',second_content='" + layoutModel.getSecondContent() + "'," +
//                "third_title='" + layoutModel.getThirdTitle() + "'," +
//                "update_time=now() where id=" + layoutModel.getId();
//        return JDBC.upDate(sql);
        return GetSqlSession.getSqlSession().update("com.cn.demo4.dao.LayoutDao.update",layoutModel);
    }

    @Override
    public int del(Integer id) {
//        String sql = "delete from layout where id=" + id;
//        return JDBC.upDate(sql);
        return GetSqlSession.getSqlSession().delete("com.cn.demo4.dao.LayoutDao.del",id);
    }

    @Override
    public List<LayoutModel> getLayoutModelListForLayout() {
//        String sql = "select * from layout where status=1";
//        ResultSet resultSet = JDBC.select(sql);
//        return getData(resultSet);
        return GetSqlSession.getSqlSession().selectList("com.cn.demo4.dao.LayoutDao.getLayoutModelListForLayout");
    }

    @Override
    public int getCount(LayoutModel layoutModel) {
//        StringBuilder sql = new StringBuilder();
//        sql.append("select count(*) count from layout where true ");
//        if (!"".equals(layoutModel.getTitle())) {
//            sql.append(" and title like '%").append(layoutModel.getTitle()).append("%'");
//        }
//        if (!"".equals(layoutModel.getContent())) {
//            sql.append(" and content like '%").append(layoutModel.getContent()).append("%'");
//        }
//        if (!"".equals(layoutModel.getStartTime()) && "".equals(layoutModel.getEndTime())) {
//            sql.append(" and update_time >= '").append(layoutModel.getStartTime()).append("' ");
//        }
//        if ("".equals(layoutModel.getStartTime()) && !"".equals(layoutModel.getEndTime())) {
//            sql.append(" and update_time <= '").append(layoutModel.getEndTime()).append("' ");
//        }
//        if (!"".equals(layoutModel.getStartTime()) && !"".equals(layoutModel.getEndTime())) {
//            sql.append(" and update_time between '").append(layoutModel.getStartTime()).append("' and '").append(layoutModel.getEndTime()).append("'");
//        }
//        if (layoutModel.getStatus() != -1) {
//            if (layoutModel.getStatus() == 1) {
//                sql.append(" and status=1");
//            } else {
//                sql.append(" and status is null and status=0");
//            }
//        }
//        sql.append(" order by id desc");
//        ResultSet resultSet = JDBC.select(sql.toString());
//        int count = 0;
//        try {
//            while (resultSet.next()) {
//                count = resultSet.getInt("count");
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return count;
        return GetSqlSession.getSqlSession().selectOne("com.cn.demo4.dao.LayoutDao.getCount",layoutModel);
    }


    private List<LayoutModel> getData(ResultSet resultSet) {
        List<LayoutModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                LayoutModel layoutModel = new LayoutModel();
                layoutModel.setId(resultSet.getInt("id"));
                layoutModel.setTitle(resultSet.getString("title"));
                layoutModel.setImg(resultSet.getString("img"));
                layoutModel.setContent(resultSet.getString("content"));
                layoutModel.setSecondContent(resultSet.getString("second_content"));
                layoutModel.setSecondTitle(resultSet.getString("second_title"));
                layoutModel.setThirdTitle(resultSet.getString("third_title"));
                layoutModel.setCreateTime(resultSet.getString("create_time"));
                layoutModel.setUpdateTime(resultSet.getString("update_time"));
                layoutModel.setStatus(resultSet.getInt("status"));
                list.add(layoutModel);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
